package com.tuanzi.blog.back.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tuanzi.blog.back.bean.Article;
import com.tuanzi.blog.back.bean.Category;
import com.tuanzi.blog.back.bean.Tag;
import com.tuanzi.blog.back.bean.User;
import com.tuanzi.blog.back.service.ArticleService;
import com.tuanzi.blog.base.bean.ResultVo;
import com.tuanzi.blog.base.exception.BlogException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 *  这个是针对文章的控制类
 */

@Controller
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 分页查询文章
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/article/list")
    @ResponseBody
    public PageInfo<Article> list(int page, int pageSize, String title, HttpSession session){
        //获取当前登录的用户
        User user = (User) session.getAttribute("user");
        //参数1：当前页码 参数2：每页记录数pageSize 该方法等同与 limit a,b
        PageHelper.startPage(page,pageSize);
        List<Article> articles = articleService.list(user.getUid(),title);
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        return pageInfo;
    }

    /**
     * 这里写一个方法，用来更改文章是否公开
     */
    @RequestMapping(value = "/article/isOpen")
    @ResponseBody
    public ResultVo ipOpen(Article article){

        ResultVo resultVo = new ResultVo();
        try{
            articleService.isOpen(article);
            resultVo.setOk(true);
            if (article.getIsOpen().equals("0")){
                resultVo.setMess("文章已修改为私密！！！");
            }else {
                resultVo.setMess("文章已公开！！！");
            }
        }catch (BlogException e){
            resultVo.setOk(false);
            return resultVo;
        }
        return resultVo;
    }

    /**
     * 异步查询所有栏目
     */
    @RequestMapping(value = "/article/queryCategory")
    @ResponseBody
    public List<Category> queryCategory(){

        List<Category> categoryList = articleService.queryCategory();

        return categoryList;
    }

    /**
     * 加载栏目下面对应的标签
     */
    @RequestMapping(value = "/article/queryTags")
    @ResponseBody
    public List<Tag> queryTags(String cid){
        List<Tag> tags = articleService.queryTags(cid);
        return tags;
    }

    /**
     * 异步发布文章
     */
    @RequestMapping(value = "/article/saveOrUpdate")
    @ResponseBody
    public ResultVo saveOrUpdate(Article article,HttpSession session){

        ResultVo resultVo = new ResultVo();
        try{
            //获取登录用户
            User user = (User) session.getAttribute("user");
            article.setUid(user.getUid());
            article = articleService.saveOrUpdate(article);
            resultVo.setOk(true);
            if(article.getAid() == null){
                resultVo.setMess("发布文章成功");
            }else{
                resultVo.setMess("修改文章文章成功");
            }
        }catch (BlogException e){
            resultVo.setOk(false);
            resultVo.setMess(e.getMessage());
            return resultVo;
        }
        return resultVo;
    }

    /**
     * 异步请求，查询当前文章信息
     */
    @RequestMapping(value = "/article/queryById")
    @ResponseBody
    public Article queryById(String id){
        Article article = articleService.queryById(id);
        return article;
    }

    /**
     * 这里进行文章的删除
     */
    @RequestMapping(value = "/article/deleteById")
    @ResponseBody
    public ResultVo deleteById(String id){

        ResultVo resultVo = new ResultVo();
        try{
            articleService.deleteById(id);
            resultVo.setOk(true);
            resultVo.setMess("删除文章成功!");
        }catch (BlogException e){
            resultVo.setOk(false);
            resultVo.setMess(e.getMessage());
        }
        return resultVo;
    }


}
